// Avoid `console` errors in browsers that lack a console.
if (!(window.console && console.log)) {
    (function() {
        var noop = function() {};
        var methods = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'markTimeline', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
        var length = methods.length;
        var console = window.console = {};
        while (length--) {
            console[methods[length]] = noop;
        }
    }());
}



$(document).ready(function() {



  	var yTValidation;
	
yTValidation = {
    selector: '[type="youtube"]',
    context: 'errors',
    statement: function(field) {
     	var matches = $(field).val().match(/watch\?v=([a-zA-Z0-9\-_]+)/);
     
  		if (!matches)
  		{

  		    return 'invalid youtube URL';
  		}
      var videoidArray = $('#label-0_field').val().split("?v=");
        var videoid = videoidArray[1];
        var jsonurl = "https://gdata.youtube.com/feeds/api/videos/"+videoid+"?v=2&prettyprint=true&alt=json";

        $.when( $.getJSON(jsonurl)).then(function(data){
          $.each( data.entry.yt$accessControl, function( key, value ) {
            var next = false;
            $.each( value, function( key2, value2 ) {
              if(next===true){
                embeddable = value2;
                if(embeddable!="allowed"){
                  return "youtube video cannot be embedded on mobile";
                }
              }
              if(value2=="embed"){
                next = true;
              }
            });
          });

        });

    }
  };

  FieldsUtils.evaluationRegistry.add(yTValidation);
  fields = Fields.in('#form-one');

  var inputsArray = ['label-0_field', 'textarea_field']
  

  function initValidation(){
    fields = Fields.in('#form-one');

    for (var i = 0; i < inputsArray.length; i++) {
      $('#form-one-' + inputsArray[i] + '-valid').text(fields.get(inputsArray[i]).isValid());
      function valid_binding(input) {
        fields.get(input).on('change:valid', function(e, val){
          $('#form-one-' + input + '-valid').text(val)
        });
      }
      valid_binding(inputsArray[i]);

      $('#form-one-' + inputsArray[i] + '-value').text(fields.get(inputsArray[i]).val());
      function value_binding(input) {
        fields.get(input).on('change:value', function(e, val){
          $('#form-one-' + input + '-value').text(val)
        });
      }
      value_binding(inputsArray[i]);

      $('#form-one-' + inputsArray[i] + '-errors').text(fields.get(inputsArray[i]).errors());
      function error_binding(input) {
        fields.get(input).on('change:value', function(e, val){
          $('#form-one-' + input + '-errors').text(fields.get(input).errors())
        });
      }
      error_binding(inputsArray[i]);
    }
    $('#form-one-valid').text(fields.isValid());
    fields.on('change:valid', function(e, val){
      $('#form-one-valid').text(fields.isValid());
    });

  }


  /*
  $('#form-one').submit(function(e){
      alert($('#form-one-label-0_field-valid').value());
      e.preventDefault()

      if(fields.isValid()){

        
        
        $.getJSON($('#form-one-label-0_field-valid').value(), function(json) {
           alert("JSON Data: " + json.name);
         });
      }
    });
*/
 

	  $('#clear-form-one').click(function(e){
	    e.preventDefault()
	    fields.clear()
	  });

    function isEmbeddable(){

    }

	  $('#form-one').submit(function(e){
      
	   if(fields.isValid()){




       /* */
	    }else{
        return false;
      }
	  });


	  initValidation();

});